package com.molyfun.parents.fragment.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SexChooseDialogFragment extends DialogFragment implements View.OnClickListener{


    @InjectView(R.id.sex_male)
    TextView mSexMale;
    @InjectView(R.id.sex_famale)
    TextView mSexfamale;


    private static SexChooseDialogFragment newInstance() {
        SexChooseDialogFragment mOrganizationOptionDialogFragment = new SexChooseDialogFragment();
        return mOrganizationOptionDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialog);
    }

    @Override public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (null != dialog) {
            dialog.getWindow().setLayout(-1, -2);
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);
        View view = inflater.inflate(R.layout.dialog_sex_option, container, false);
        ButterKnife.inject(this, view);
        mSexMale.setOnClickListener(this);
        mSexfamale.setOnClickListener(this);
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sex_male:
                if(mSexChooseCallback != null){
                    mSexChooseCallback.onSexChoose(true);
                    mSexMale.setTextColor(ContextCompat.getColor(getContext(), R.color.color_base));
                }
                break;
            case R.id.sex_famale:
                if(mSexChooseCallback != null){
                    mSexChooseCallback.onSexChoose(false);
                    mSexfamale.setTextColor(ContextCompat.getColor(getContext(), R.color.color_base));
                }
                break;
            default:
                break;
        }
        getDialog().dismiss();
    }

    public static interface SexChooseCallback{
        void onSexChoose(boolean isMale);
    }

    private void setOrganizationOptionCallback(SexChooseCallback sexChooseCallback){
        this.mSexChooseCallback = sexChooseCallback;
    }

    private SexChooseCallback mSexChooseCallback;

    public static class SexChooseDialogBuilder {

        SexChooseDialogFragment mSexChooseDialogFragment;
        private String mTag;

        public SexChooseDialogBuilder(){
            mSexChooseDialogFragment = SexChooseDialogFragment.newInstance();
            mTag = "sexchoose_option";
        }

        public SexChooseDialogBuilder setSexChooseCallback(SexChooseCallback sexChooseCallback){
            mSexChooseDialogFragment.setOrganizationOptionCallback(sexChooseCallback);
            return this;
        }

        public SexChooseDialogBuilder setTag(String tag){
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction){
            fragmentTransaction.add(mSexChooseDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }
}
